﻿package baidu.ui.controls
{
    import baidu.ui.constants.*;
    import baidu.ui.containers.*;
    import baidu.ui.controls.list.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import baidu.ui.managers.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class List extends ListBase
    {
        protected var _rowHeight:Number = 20;
        protected var _cellRenderer:Object;
        protected var _scrollPane:ScrollPane;
        public static var defaultStyles:Object = {};

        public function List()
        {
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }// end function

        override public function set enabled(param1:Boolean) : void
        {
            super.enabled = param1;
            this._scrollPane.enabled = param1;
            container.mouseChildren = _enabled;
            return;
        }// end function

        public function get scrollPane() : ScrollPane
        {
            return this._scrollPane;
        }// end function

        public function get rowHeight() : Number
        {
            return this._rowHeight;
        }// end function

        public function set rowHeight(param1:Number) : void
        {
            this._rowHeight = param1;
            invalidate(Invalidation.SIZE);
            return;
        }// end function

        public function get rowCount() : uint
        {
            return Math.ceil(this._scrollPane.height / this.rowHeight);
        }// end function

        public function set rowCount(param1:uint) : void
        {
            var _loc_2:* = Number(this._scrollPane.getStyle("padding"));
            height = this.rowHeight * param1 + 2 * _loc_2;
            return;
        }// end function

        override protected function initUI() : void
        {
            super.initUI();
            setSize(100, 100);
            this._scrollPane = new ScrollPane();
            addChild(this._scrollPane);
            this._scrollPane.hScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.vScrollPolicy = ScrollPolicy.AUTO;
            container = new Sprite();
            this._scrollPane.content = container;
            this._scrollPane.vScrollBar.addEventListener(ScrollEvent.SCROLL, this.handleScroll, false, 0, true);
            return;
        }// end function

        protected function handleScroll(event:ScrollEvent) : void
        {
            invalidate(Invalidation.SCROLL);
            return;
        }// end function

        override protected function draw() : void
        {
            var _loc_1:* = this._scrollPane.contentHeight != this.rowHeight * length;
            this._scrollPane.contentHeight = this.rowHeight * length;
            this._scrollPane.invalidate();
            this._scrollPane.drawNow();
            if (isInvalid(Invalidation.STYLES))
            {
                if (this._cellRenderer != getStyleValue("renderer"))
                {
                    _invalidateList();
                    this._cellRenderer = getStyleValue("renderer");
                }
            }
            if (isInvalid(Invalidation.SIZE, Invalidation.STATE) || _loc_1)
            {
                this.drawLayout();
                this._scrollPane.drawNow();
            }
            if (isInvalid(Invalidation.RENDERER_STYLES))
            {
                updateRendererStyles();
            }
            if (isInvalid(Invalidation.STYLES, Invalidation.SIZE, Invalidation.DATA, Invalidation.SCROLL, Invalidation.SELECTED))
            {
                this.drawList();
            }
            validate();
            return;
        }// end function

        override protected function drawList() : void
        {
            var _loc_3:int = 0;
            var _loc_4:* = undefined;
            var _loc_5:IListCell = null;
            var _loc_8:Boolean = false;
            var _loc_9:Sprite = null;
            var _loc_10:String = null;
            var _loc_1:* = Math.floor(this._scrollPane.vScrollPosition / this.rowHeight);
            var _loc_2:* = Math.min(length, _loc_1 + this.rowCount + 1);
            var _loc_11:* = new Dictionary(true);
            renderedItems = new Dictionary(true);
            var _loc_6:* = _loc_11;
            _loc_3 = _loc_1;
            while (_loc_3 < _loc_2)
            {
                
                _loc_6[_dataProvider.getItemAt(_loc_3)] = true;
                _loc_3++;
            }
            var _loc_7:* = new Dictionary(true);
            while (activeCells.length > 0)
            {
                
                _loc_5 = activeCells.pop() as IListCell;
                _loc_4 = _loc_5.data;
                if (_loc_6[_loc_4] == null || invalidItems[_loc_4] == true)
                {
                    availableCells.push(_loc_5);
                }
                else
                {
                    _loc_7[_loc_4] = _loc_5;
                    invalidItems[_loc_4] = true;
                }
                container.removeChild(_loc_5 as DisplayObject);
            }
            invalidItems = new Dictionary(true);
            _loc_3 = _loc_1;
            while (_loc_3 < _loc_2)
            {
                
                _loc_8 = false;
                _loc_4 = _dataProvider.getItemAt(_loc_3);
                if (_loc_7[_loc_4] != null)
                {
                    _loc_8 = true;
                    _loc_5 = _loc_7[_loc_4];
                    delete _loc_7[_loc_4];
                }
                else if (availableCells.length > 0)
                {
                    _loc_5 = availableCells.pop() as IListCell;
                }
                else
                {
                    _loc_5 = InstanceManager.createInstance(getStyleValue("renderer")) as IListCell;
                    _loc_9 = _loc_5 as Sprite;
                    if (_loc_9 != null)
                    {
                        _loc_9.addEventListener(MouseEvent.CLICK, handleCellClick, false, 0, true);
                        _loc_9.addEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false, 0, true);
                        _loc_9.addEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false, 0, true);
                        _loc_9.addEventListener(Event.CHANGE, handleCellChange, false, 0, true);
                        _loc_9.doubleClickEnabled = true;
                        _loc_9.addEventListener(MouseEvent.DOUBLE_CLICK, handleCellDoubleClick, false, 0, true);
                        if (_loc_9["setStyle"] != null)
                        {
                            for (_loc_10 in rendererStyles)
                            {
                                
                                var _loc_13:* = _loc_9;
                                _loc_13._loc_9["setStyle"](_loc_10, rendererStyles[_loc_10]);
                            }
                        }
                    }
                }
                container.addChild(_loc_5 as Sprite);
                activeCells.push(_loc_5);
                (_loc_5 as BUI).y = this.rowHeight * _loc_3;
                (_loc_5 as BUI).setSize(this._scrollPane.availableWidth, this.rowHeight);
                if (!_loc_8)
                {
                    _loc_5.data = _loc_4;
                }
                _loc_5.listData = new ListData({index:_loc_3, owner:this});
                _loc_5.selected = _selectedIndices.indexOf(_loc_3) != -1;
                if (_loc_5 is BUI)
                {
                    (_loc_5 as BUI).drawNow();
                }
                _loc_3++;
            }
            return;
        }// end function

        override protected function drawLayout() : void
        {
            this._scrollPane.setSize(_width, _height);
            return;
        }// end function

        override public function scrollToIndex(param1:int) : void
        {
            drawNow();
            var _loc_2:* = Math.floor((this._scrollPane.vScrollPosition + this._scrollPane.availableHeight) / this.rowHeight) - 1;
            var _loc_3:* = Math.ceil(this._scrollPane.vScrollPosition / this.rowHeight);
            if (param1 < _loc_3)
            {
                this._scrollPane.vScrollPosition = param1 * this.rowHeight;
            }
            else if (param1 > _loc_2)
            {
                this._scrollPane.vScrollPosition = (param1 + 1) * this.rowHeight - this._scrollPane.availableHeight;
            }
            return;
        }// end function

    }
}
